﻿' Copyright (c) Microsoft Corporation.  All rights reserved.

Namespace MyTravelPostModel
    ' I added a property BlogWriter which allows for the Person
    ' entity to be serialized with the blog.
    Partial Public Class Blog
        ' The person that created the blog.
        <DataMemberAttribute()> _
        Public Property BlogWriter() As Person
            Get
                ' if the cached person is null and the person reference
                ' is loaded, then set the person reference.
                If PersonReference.IsLoaded Then
                    Return Person
                Else
                    Return _cachedPerson
                End If

            End Get
            Set(ByVal value As Person)
                _cachedPerson = value
            End Set
        End Property

        Private _cachedPerson As Person
    End Class


    ' The blog post should have the comments associated, most of the 
    ' time the user will need this.
    Partial Public Class BlogPost


        ' if the blog post has loaded comments, this returns the list
        ' and caches the list separatly. The trick happens when the
        ' deserialization occurs, that the user needs to merge the
        ' two lists together in their own logic.

        <DataMemberAttribute()> _
        Public Property PostComments() As List(Of Comment)
            Get
                ' do a check to make sure that the post comments
                ' are in sync. This is just for the service.
                If Comments.IsLoaded Then
                    Return Comments.ToList()
                Else
                    Return _cachedComments
                End If

                Return _cachedComments
            End Get
            Set(ByVal value As List(Of Comment))
                _cachedComments = value
            End Set

        End Property

        Private _cachedComments As List(Of Comment)
    End Class


    ' With comments, you need the person writing the comments.
    Partial Public Class Comment

        ' The comment writer
        <DataMemberAttribute()> _
        Public Property CommentWriter() As Person
            Get
                ' if the cached person is null and the person reference
                ' is loaded, then set the person reference.
                If PersonReference.IsLoaded Then
                    Return Person
                Else
                    Return _cachedPerson
                End If
            End Get
            Set(ByVal value As Person)
                _cachedPerson = value
            End Set
        End Property

        Private _cachedPerson As Person

    End Class
End Namespace
